package com.jweb.framework.util.log4j;

import java.util.concurrent.atomic.AtomicInteger;

import org.apache.log4j.net.SocketAppender;
import org.apache.log4j.spi.LoggingEvent;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jweb.framework.util.FormatUtil;

/**
 * 自定义日志Appender,用于日志采集过滤，增强。
 * 
 * @author Yuanqy
 *
 */
public class CustomSocketAppender extends SocketAppender {
	private static volatile AtomicInteger logCount = new AtomicInteger(0); // 间断采集数量
 
	
	@Override
	public void append(LoggingEvent event) {
		boolean bo = false;
		try {
			JSONObject obj = JSON.parseObject((String) event.getMessage());
			if (FormatUtil.isEmpty((obj).get("msgId")) || FormatUtil.isEmpty((obj).get("bizCode"))) {
				bo = false;
			} else {
				bo = true;
			}
		} finally {
			if (bo) {
				event.setProperty("rootPath", getClass().getResource("/").getFile().toString());
				/**
				 * TODO 追加其他附属信息，暂时没想到
				 */
				super.append(event);
				System.out.println("发送成功："+logCount.addAndGet(1));
			} else {
				return;
			}
		}
	}
}
